<?php
/**
 * Hello Controller for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

require_once( JPATH_COMPONENT.DS.'views' .DS. 'details1add' . DS . 'view.html.php' );

/**
 * Hello Hello Controller
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class TargetsControllerDetails1 extends TargetsController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	var $type_id;
	var $target_id;
	var $model;
	
	 
	function __construct()
	{
		parent::__construct();
		
		#$this->addViewPath('details1add');
		
		$this->view = $this->getView('details1add');
		$this->model = $this->getModel('details11');
		
		$this->view->setModel($this->model, true);	
		
		$model2 = $this->getModel('details1');
		$this->view->setModel($model2);	

		// Register Extra tasks
		$this->registerTask( 'add', 'edit' );
		
		$this->type_id   = JRequest::getInt('type_id');
		$this->target_id = JRequest::getInt('target_id');
	}
	
	#function display() {
	#	JRequest::setVar('view', 'details1');
	#	parent::display();
	#}

	/**
	 * display the edit form
	 * @return void
	 */
	function edit()
	{					
		
		JRequest::setVar( 'view', 'details1add' );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		#$f = fopen('test_.txt',);
		#fwrite($f, '1');
		#fclose($f);
		#$model = $this->getModel('details11');
		$details = $this->getModel('details1');
		$type_name = $details->getHeader2();
		
		
		$model = $this->model;
		
		if ($model->store($post)) {
			$msg = JText::_( "Сохранёно" );
		} else {
			$msg = JText::_( 'Ошибка при сохранении' );
		}

		// Check the table in so it can be edited.... we are done with it anyway*/
		$link = 'index.php?option=com_targets&view=details1&type_id=' . $this->type_id . '&target_id=' . $this->target_id;
		$this->setRedirect($link, $msg);
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		/*$model = $this->getModel('target');
		if(!$model->delete()) {
			$msg = JText::_( 'Error: one or more targets Could not be Deleted' );
		} else {
			$msg = JText::_( 'Target(s) Deleted' );
		}*/

		#$msg = '';
		#$this->setRedirect( 'index.php?option=com_targets', $msg );
	
		
		$model = $this->model;
		
		$model = $this->getModel('details11');
		if (!$model->delete()) {
			$msg = JText::_( "Нельзя удалить" );
		} else {
			$msg = JText::_( "Удалено" );
		}
		
		#$type_id   = JRequest::getInt('type_id');
		#$target_id = JRequest::getInt('target_id');
		
		$this->setRedirect( 'index.php?option=com_targets&view=details1&type_id=' . $this->type_id . '&target_id=' . $this->target_id, $msg );
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_( "Операция отменена" );
		$this->setRedirect( 'index.php?option=com_targets&view=details1&type_id=' . $this->type_id . '&target_id=' . $this->target_id, $msg );
	}
}